<?php
namespace app\admin\model;
use think\Model;
use think\Db;

use think\facade\Cache ;
use think\facade\Config;
/**
 * 通道报表模型 文件复制于BusOrderReport.php
 * @package app\admin\model
 */
class PlatReportFormM extends Model
{

	public function findReport($wheret,$pla = '')
	{
		// if(!cache($wheret[0].'p_GetPlatReport') )
		// {
		// 	$data = Db::query("CALL p_GetPlatReport('{$wheret[0]}','{$wheret[1]}')");

		// 	if($data){
		// 		$data = $data[0];
		// 		foreach ($data as $key => $value) {
		// 			$data[$key]['platfrom'] = $value['plat_id'] . '/' . $value['platfrom'];
		// 		}
		// 	}

		// 	$cache['time'] = '数据截取时间：' . date('Y-m-d H:i:s') . '(刷新时间10分钟)';
		// 	$cache['data'] = $data;

		// 	cache($wheret[0].'p_GetPlatReport' , $cache , 10*60);
		// }
		// dump($wheret[0].'p_GetPlatReport');
		// dump(Cache::tag('p_GetPlatReport')->get($wheret[0].'p_GetPlatReport'));
		$dataall = [];

		$wheret = str_replace("-","",$wheret);

		
		if($pla){
			$platfromList = Db::table('bus_platfrom')->where('plat_id',$pla)->field('plat_id,platfrom')->select();
		}else{
			$platfromList = Db::table('bus_platfrom')->field('plat_id,platfrom')->select();
		}

		$config = Config::get('ZD_REDIS_CONFIG');

		$redis = redisCurl($config['IP'],$config['PORT'],$config['PASSWORD'],6);

		//分运营商
		$operators = [
			['name' => '移动' , 'host' => '_10086']
			,['name' => '联通' , 'host' => '_10010']
			,['name' => '电信' , 'host' => '_10000']
			,['name' => '三网' , 'host' => '_90001']
			,['name' => '三网' , 'host' => '']
		];

		// 汇总
		$allone['platfrom']    = '汇总';
        $allone['plat_id']             = 0;
        $allone['sms_all']         = 0;
        $allone['sms_success']     = 0;
        $allone['sms_faild']       = 0;
        $allone['sms_sending']     = 0;
        $allone['amt_sms_success'] = 0;

		foreach ($platfromList as $key => $value) {

			$platsms = [];
			//单个通道汇总
			$platsms['platfrom']        = $value['plat_id'] . '/' . $value['platfrom'];
	        $platsms['plat_id']         = $value['plat_id'];
	        $platsms['sms_all']         = 0;
	        $platsms['sms_success']     = 0;
	        $platsms['sms_faild']       = 0;
	        $platsms['sms_sending']     = 0;
	        $platsms['amt_sms_success'] = 0;
	        $platsms['isp_host'] = '汇总';
	        $platsms['data'] = [];
			for ($i=0; $i < count($operators); $i++) { 
				$dataOne = [];

				$qianz = 'REPORT_P_' . $wheret . '_' . $value['plat_id'] . $operators[$i]['host'] . '_';
				// $qianz = 'REPORT_P_' . $wheret . '_' . $value['plat_id'] . '_';

				$sms_all = $redis->get($qianz . 'PT');
				if($sms_all)
				{
					$sms_success = $redis->get($qianz . 'PS');
					if(!$sms_success){
						$sms_success = 0;
					}
					$sms_faild = $redis->get($qianz . 'PF');
					if(!$sms_faild){
						$sms_faild = 0;
					}
					$amt_sms_success = $redis->get($qianz . 'PM');
					if(!$amt_sms_success){
						$amt_sms_success = 0;
					}

					$dataOne['platfrom']    = $value['plat_id'] . '/' . $value['platfrom'];
			        $dataOne['plat_id']             = $value['plat_id'];
			        $dataOne['sms_all']         = $sms_all;
			        $dataOne['sms_success']     = $sms_success;
			        $dataOne['sms_faild']       = $sms_faild;
			        $dataOne['sms_sending']     = $sms_all - ( $sms_success + $sms_faild );
			        $dataOne['amt_sms_success'] = round($amt_sms_success/1000,3);

			        //百分比
					$dataOne['sms_success100'] = round(($dataOne['sms_success']/$dataOne['sms_all'])*100,3) . '%';
					$dataOne['sms_faild100']   = round(($dataOne['sms_faild']/$dataOne['sms_all'])*100,3) . '%';
					$dataOne['sms_sending100'] = round(($dataOne['sms_sending']/$dataOne['sms_all'])*100,3) . '%';
					
					$dataOne['isp_host'] = $operators[$i]['name'];

			        $platsms['data'][] = $dataOne;
			        // halt($platsms);

			        //渠道汇总
			        $platsms['sms_all']         += $dataOne['sms_all'];
			        $platsms['sms_success']     += $dataOne['sms_success'];
			        $platsms['sms_faild']       += $dataOne['sms_faild'];
			        $platsms['sms_sending']     += $dataOne['sms_sending'];
			        $platsms['amt_sms_success'] += $dataOne['amt_sms_success'];
			        //汇总
			        $allone['sms_all']         += $dataOne['sms_all'];
			        $allone['sms_success']     += $dataOne['sms_success'];
			        $allone['sms_faild']       += $dataOne['sms_faild'];
			        $allone['sms_sending']     += $dataOne['sms_sending'];
			        $allone['amt_sms_success'] += $dataOne['amt_sms_success'];
				}
			}
			//通道汇总
			if($platsms['sms_all'] > 0)
			{
				//汇总百分比
				$platsms['sms_success100']  = round(($platsms['sms_success']/$platsms['sms_all'])*100,3) . '%';
				$platsms['sms_faild100']    = round(($platsms['sms_faild']/$platsms['sms_all'])*100,3) . '%';
				$platsms['sms_sending100']  = round(($platsms['sms_sending']/$platsms['sms_all'])*100,3) . '%';
				$platsms['amt_sms_success'] = round($platsms['amt_sms_success'],3);
				$dataall[] = $platsms;
			}
		}

		//输出的数据
		$returnData = [];

		if(count($dataall) > 0)
		{        
			//排序
			$smsSuccessList = array_column($dataall,'sms_success');
			array_multisort($smsSuccessList,SORT_DESC,$dataall);

			// 把运营商放出来
			foreach ($dataall as $kd => $vd) {
				$data = $vd['data'];
				unset($vd['data']);

				$returnData[] = $vd;

				foreach ($data as $k => $v) {
					$returnData[] = $v;
				}
			}
			unset($dataall);
			//汇总百分比
			$allone['sms_success100']  = round(($allone['sms_success']/$allone['sms_all'])*100,3) . '%';
			$allone['sms_faild100']    = round(($allone['sms_faild']/$allone['sms_all'])*100,3) . '%';
			$allone['sms_sending100']  = round(($allone['sms_sending']/$allone['sms_all'])*100,3) . '%';
			$allone['amt_sms_success'] = round($allone['amt_sms_success'],3);
			array_unshift($returnData, $allone);
		}

		return $returnData;
	}

	public function findReportPlat($plat_id,$wheret)
	{
		$dataall = [];

		$wheret = str_replace("-","",$wheret);

		$platfrom = Db::table('bus_platfrom')->field('plat_id,platfrom')->where('plat_id',$plat_id)->field(true)->find();

		//获取配置
		$config = Config::get('ZD_REDIS_CONFIG');

		//连接redis
		$redis = redisCurl($config['IP'],$config['PORT'],$config['PASSWORD'],6);

		//指定前缀
		$qianz = 'REPORT_P_' . $wheret . '_' . $platfrom['plat_id'] . '_';

		$sms_all = $redis->get($qianz . 'PT');
		if(!$sms_all){
			$sms_all = 0;
		}
		//获取成功数
		$sms_success = $redis->get($qianz . 'PS');
		if(!$sms_success){
			$sms_success = 0;
		}
		//失败数
		$sms_faild = $redis->get($qianz . 'PF');
		if(!$sms_faild){
			$sms_faild = 0;
		}
		//总金额
		$amt_sms_success = $redis->get($qianz . 'PM');
		if(!$amt_sms_success){
			$amt_sms_success = 0;
		}

		$dataall['platfrom']        = $platfrom['platfrom'];
        $dataall['plat_id']         = $platfrom['plat_id'];
        $dataall['sms_all']         = $sms_all;
        $dataall['sms_success']     = $sms_success;
        $dataall['sms_faild']       = $sms_faild;
        $dataall['sms_sending']     = $sms_all - ( $sms_success + $sms_faild );
        $dataall['amt_sms_success'] = $amt_sms_success/1000;

        return $dataall;
	}

	public function getOrderReportYueArray($request){
		$bus_order_report = Db::table('bus_order_plat_report')
		->field('bus_order_plat_report.plat_id,bus_order_plat_report.isp_host,bus_platfrom.platfrom,sum(sms_all) sms_all,sum(sms_success) sms_success,sum(sms_faild) sms_faild,sum(sms_sending) sms_sending,sum(amt_sms_success) amt_sms_success')
		->join('bus_platfrom','bus_order_plat_report.plat_id=bus_platfrom.plat_id','LEFT')
		->where("date_format(bus_order_plat_report.create_time,'%Y-%m')='{$request['seletime']}'");
		if($request['pla']){
			$bus_order_report = $bus_order_report->where('bus_order_plat_report.plat_id',$request['pla']);
		}
		$bus_order_report = $bus_order_report->group('bus_order_plat_report.plat_id,bus_order_plat_report.isp_host')
		->order('amt_sms_success desc , bus_order_plat_report.id desc')
		->select();

		$add['platfrom'] = '汇总';
		$add['sms_all']         = 0;
		$add['sms_success']     = 0;
		$add['sms_faild']       = 0;
		$add['sms_sending']     = 0;
		$add['amt_sms_success'] = 0;

		//把数据装进去
		$longdata = [];
		// foreach ($bus_order_report as $key => $value) {
		// 	$bus_order_report[$key]['company_name'] = $value['channel_id'].'/'.$value['company_name'];
		// 	$bus_order_report[$key]['cid'] = $value['channel_id'];

		// 	if(!$value['isp_host'])
		// 	{
		// 		$bus_order_report[$key]['isp_host'] = '三网';
		// 	}

		// 	$longdata[$value['channel_id']][] = $bus_order_report[$key];
		// 	//汇总
		// 	$add['sms_all']         = bcadd($add['sms_all'],$bus_order_report[$key]['sms_all']);
		// 	$add['sms_success']     = bcadd($add['sms_success'],$bus_order_report[$key]['sms_success']);
		// 	$add['sms_faild']       = bcadd($add['sms_faild'],$bus_order_report[$key]['sms_faild']);
		// 	$add['sms_sending']     = bcadd($add['sms_sending'],$bus_order_report[$key]['sms_sending']);
		// 	$add['amt_sms_success'] = bcadd($add['amt_sms_success'],$bus_order_report[$key]['amt_sms_success'],5);
		// }


		foreach ($bus_order_report as $key => $value) {
			$bus_order_report[$key]['platfrom'] = $value['plat_id'].'/'.$value['platfrom'];
			$bus_order_report[$key]['plat_id'] = $value['plat_id'];

			if(!$value['isp_host'])
			{
				$bus_order_report[$key]['isp_host'] = '三网';
			}
				
			$longdata[$value['plat_id'].'a'][] = $bus_order_report[$key];
			//汇总
			$add['sms_all']         = bcadd($add['sms_all'],$bus_order_report[$key]['sms_all']);
			$add['sms_success']     = bcadd($add['sms_success'],$bus_order_report[$key]['sms_success']);
			$add['sms_faild']       = bcadd($add['sms_faild'],$bus_order_report[$key]['sms_faild']);
			$add['sms_sending']     = bcadd($add['sms_sending'],$bus_order_report[$key]['sms_sending']);
			$add['amt_sms_success'] = bcadd($add['amt_sms_success'],$bus_order_report[$key]['amt_sms_success'],5);
		}
		
		$bus_order_report = [];
		foreach ($longdata as $key => $value) {
			
			$huiz['isp_host'] = '汇总';
			$huiz['sms_all']         = 0;
			$huiz['sms_success']     = 0;
			$huiz['sms_faild']       = 0;
			$huiz['sms_sending']     = 0;
			$huiz['amt_sms_success'] = 0;

			for ($j=0; $j < count($value); $j++) { 
				//汇总
				$huiz['plat_id'] = $value[$j]['plat_id'];
				$huiz['platfrom'] = $value[$j]['platfrom'];
				$huiz['sms_all']         = bcadd($huiz['sms_all'],$value[$j]['sms_all']);
				$huiz['sms_success']     = bcadd($huiz['sms_success'],$value[$j]['sms_success']);
				$huiz['sms_faild']       = bcadd($huiz['sms_faild'],$value[$j]['sms_faild']);
				$huiz['sms_sending']     = bcadd($huiz['sms_sending'],$value[$j]['sms_sending']);
				$huiz['amt_sms_success'] = bcadd($huiz['amt_sms_success'],$value[$j]['amt_sms_success'],5);
			}
			//添加汇总
			$value[] = $huiz;

			//金额排序
			$last_names = array_column($value,'amt_sms_success');
			array_multisort($last_names,SORT_DESC,$value);

			$bus_order_report = array_merge_recursive($bus_order_report,$value);
		}
		//金额排序
		// $last_names = array_column($bus_order_report,'amt_sms_success');
		// array_multisort($last_names,SORT_DESC,$bus_order_report);

		//汇总添加到第一项
		array_unshift($bus_order_report,$add);

		return $bus_order_report;
	}

		/**
	 * 单渠道月报表
	 * @Author   lf
	 * @DateTime 2019-10-16T11:25:16+0800
	 * @param    [type]                   $request [description]
	 * @return   [type]                            [description]
	 */
	public function getOrderReportYueCidArray($request)
	{
		$bus_order_report = Db::table('bus_order_plat_report')
		->field('bus_platfrom.plat_id
			,bus_platfrom.platfrom
			,bus_order_plat_report.isp_host
			,IFNULL(sum(sms_all),0) sms_all
			,IFNULL(sum(sms_success),0) sms_success
			,IFNULL(sum(sms_faild),0) sms_faild
			,IFNULL(sum(sms_sending),0) sms_sending
			,IFNULL(sum(amt_sms_success),0) amt_sms_success'
			)
		->join('bus_platfrom','bus_order_plat_report.plat_id=bus_platfrom.plat_id')
		->where("date_format(bus_order_plat_report.create_time,'%Y-%m')='{$request['seletime']}'")
		->where('bus_order_plat_report.plat_id',$request['plat_id'])
		->order('bus_order_plat_report.id desc')
		->find();

		return $bus_order_report;
	}
}